package com.sairobo.admin.modules.srblive.manager.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sairobo.base.common.constant.LiveConstant;
import com.sairobo.base.common.utils.ShiroUtils;
import com.sairobo.base.common.constant.QueryConts;
import com.sairobo.base.common.entity.Page;
import com.sairobo.base.common.entity.Query;
import com.sairobo.admin.modules.srblive.dao.SrbliveClassroomMapper;
import com.sairobo.admin.modules.srblive.entity.SrbliveClassroom;
import com.sairobo.admin.modules.srblive.entity.SrbliveUser;
import com.sairobo.admin.modules.srblive.service.SrbliveUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sairobo.admin.modules.srblive.manager.SrbliveClassroomManager;

/**
 * 课题
 *
 * @author admin
 * @email sairobo
 * @url www.sairobo.com
 * @date 2018年1月11日 上午11:01:21
 */
@Component("srbliveClassroomManager")
public class SrbliveClassroomManagerImpl extends ServiceImpl<SrbliveClassroomMapper,SrbliveClassroom> implements SrbliveClassroomManager {

    @Autowired
    SrbliveUserService srbliveUserService;

	@Override
	@SuppressWarnings("unchecked")
	public List<SrbliveClassroom> listSrbliveClassroom(Page<SrbliveClassroom> page, Query search) {
        EntityWrapper<SrbliveClassroom> ew=new EntityWrapper<SrbliveClassroom>();
        Map map = (HashMap)search.get(QueryConts.SEARCH_KEYWORD);
        ew.like("title",(String) map.get("key"));

        if(ShiroUtils.getUserEntity().getRoleCodeList().contains(LiveConstant.ADMIN_ROLE_CODE)){
            //管理员能看到所有数据
        }else {
            SrbliveUser srbliveUser = srbliveUserService.getByunionId(ShiroUtils.getUserEntity().getUnionId());
            ew.eq("openId",srbliveUser!=null?srbliveUser.getOpenid():null);
        }
        //根据openId过滤
//        if(StringUtils.isNotEmpty(ShiroUtils.getUserEntity().getOpenId())){
//            ew.eq("openId", ShiroUtils.getUserEntity().getOpenId());
//        }

        ew.orderBy("id",false);
        return baseMapper.selectPage(page,ew);
	}

	@Override
	public int saveSrbliveClassroom(SrbliveClassroom srbliveClassroom) {
        return baseMapper.insert(srbliveClassroom);
	}

	@Override
	public SrbliveClassroom getSrbliveClassroomById(Long id) {
        SrbliveClassroom test = baseMapper.selectById(id);
        return test;
	}

	@Override
	public int updateSrbliveClassroom(SrbliveClassroom srbliveClassroom) {
        return baseMapper.updateById(srbliveClassroom);
	}

	@Override
	public int batchRemove(Long[] id) {
        int count = baseMapper.deleteBatchIds(Arrays.asList(id));
        return count;
	}
	
}
